package com.example.servlet;

import com.example.DAO.DBUtilsDAO;
import com.example.DAO.impl.DBUtilsFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/addOrders"})
public class AddOrderServlet extends HttpServlet {
    private DBUtilsDAO dbConnection = DBUtilsFactory.getDBConnection();

    public AddOrderServlet() {
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int order_id = Integer.parseInt(req.getParameter("order_id"));
        String product_image = req.getParameter("product_image");
        String product_name = req.getParameter("product_name");
        String user_name = req.getParameter("user_name");
        String order_date = req.getParameter("order_date");
        int quantity = Integer.parseInt(req.getParameter("quantity"));
        String product_details = req.getParameter("product_details");
        double unitPrice = Double.parseDouble(req.getParameter("price"));

        String sqlSelect = "SELECT COUNT(*) FROM orders WHERE product_name = ? AND user_name = ?";
        String sqlInsert = "INSERT INTO orders (order_id, product_image, product_name, user_name, status, order_date, product_details, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        String sqlUpdate = "UPDATE orders SET order_date=?, quantity=?, total_price=? WHERE product_name=? AND user_name=? AND order_id=?";

        try (Connection conn = this.dbConnection.getConnection()) {
            try (PreparedStatement prepSelect = conn.prepareStatement(sqlSelect)) {
                prepSelect.setString(1, product_name);
                prepSelect.setString(2, user_name);
                ResultSet rs = prepSelect.executeQuery();
                if (rs.next() && rs.getInt(1) > 0) {

                    try (PreparedStatement prepUpdate = conn.prepareStatement(sqlUpdate)) {
                        prepUpdate.setString(1, order_date);
                        prepUpdate.setInt(2, quantity);
                        prepUpdate.setDouble(3, unitPrice * quantity);
                        prepUpdate.setString(4, product_name);
                        prepUpdate.setString(5, user_name);
                        prepUpdate.setInt(6, order_id);
                        prepUpdate.executeUpdate();
                        resp.sendRedirect("customer.jsp");
                    }
                } else {

                    try (PreparedStatement prepInsert = conn.prepareStatement(sqlInsert)) {
                        prepInsert.setInt(1, order_id);
                        prepInsert.setString(2, product_image);
                        prepInsert.setString(3, product_name);
                        prepInsert.setString(4, user_name);
                        prepInsert.setString(5, "待发货");
                        prepInsert.setString(6, order_date);
                        prepInsert.setString(7, product_details);
                        prepInsert.setInt(8, quantity);
                        prepInsert.setDouble(9, unitPrice);
                        prepInsert.setDouble(10, unitPrice * quantity);
                        int affectedRows = prepInsert.executeUpdate();
                        if (affectedRows > 0) {
                            resp.sendRedirect("customer.jsp");
                        } else {
                            req.setAttribute("order_warn", "用户" + user_name + "的订单生成失败");
                            req.getRequestDispatcher("/customer.jsp").forward(req, resp);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            req.setAttribute("error", "数据库操作失败");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }
    }
}